# encoding: UTF-8
"""

@author = 李林名
@email = lishuai1199@qq.com
@create_time = 2022/11/24 10:47

"""

from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep


# #1.选择iframe中类别为plant的元素:土豆,洋葱,白菜
# # css selector选择器不会选择iframe和frame的元素
# wd = webdriver.Chrome()
# wd.get('https://cdn2.byhy.net/files/selenium/sample2.html')
# ## 切换到内部frame:对应的id或者name
# wd.switch_to.frame('frame1')
# ## 根据属性选择元素
# elements = wd.find_elements(By.CSS_SELECTOR, '.plant span')
# ## 打印出元素对应的html
# for element in elements:
#     print(element.get_attribute('innerHTML'))
# sleep(1)


# #2.使用属性获取iframe，选择iframe中类别为plant的元素:土豆,洋葱,白菜
# # css selector选择器不会选择iframe和frame的元素
# wd = webdriver.Chrome()
# wd.get('https://cdn2.byhy.net/files/selenium/sample2.html')
# ## 切换到内部frame:对应的id或者name
#
# wd.switch_to.frame(wd.find_element(By.CSS_SELECTOR,'[src="sample1.html"]'))
# ## 根据属性选择元素
# elements = wd.find_elements(By.CSS_SELECTOR, '.plant span')
# ## 打印出元素对应的html
# for element in elements:
#     print(element.get_attribute('innerHTML'))
# sleep(1)


#3.退出进入的frame，选择之前的元素: 下面的内容是iframe中的
# css selector选择器不会选择iframe和frame的元素
wd = webdriver.Chrome()
wd.get('https://cdn2.byhy.net/files/selenium/sample2.html')
## 切换到内部frame:对应的id或者name
wd.switch_to.frame(wd.find_element(By.CSS_SELECTOR,'[src="sample1.html"]'))
## 根据属性选择元素
elements = wd.find_elements(By.CSS_SELECTOR, '.plant span')
## 打印出元素对应的html
for element in elements:
    print(element.get_attribute('innerHTML'))

wd.switch_to.default_content()
element=wd.find_element(By.CSS_SELECTOR,'.baiyueheiyu span')
print(element.get_attribute('innerHTML'))
sleep(1)
